# -*- coding: utf-8 -*-

#在Python中利用Playwright库模拟网络请求，以测试前端在服务器异常等不同场景下的表现。
# 通过拦截和修改API响应，可以创建各种错误条件，例如模拟登录时的服务器异常（HTTP 500状态码），进而全面测试前端功能

from playwright.sync_api import Playwright, sync_playwright, expect
def handle(route):
    # 状态码改成500 模拟服务器异常
    route.fulfill(status=500)
    # response=route.fulfill()
    # json=response.json();
    # json["message"]["big_red_dog"]=[]
    # route.fulfill(response=response,json=json)


def run(playwright: Playwright) -> None:

    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto('http://192.168.7.102/users/sign_in')
    page.get_by_label("Username or email").fill("yangxc@it.lianfa.cn")
    page.get_by_label("Password").fill("victal2422")
    #page.route拦截请求，模拟返回500
    page.route('http://192.168.7.102/users/sign_in',handle)
    page.get_by_role("button", name="Sign in").click()

    page.pause()  # 断点
    # ---------------------
    context.close()
    browser.close()

if __name__ == '__main__':

    with sync_playwright() as playwright:
        run(playwright)

